﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Data;
using System.Reflection;
namespace UniMana.Win.Training
{
    /// <summary>
    /// Chức năng yêu cầu môn, duyệt, khóa, xếp phòng
    /// 
    /// Tổng quan các chức năng
    /// - Yêu cầu mở môn => Training_SubjectOpenRequest
    /// - Mở môn học => Training_SubjectOpen
    /// - Lớp học phần => Training_Subject_Class
    /// - Sinh viên thuộc lớp học phần => Training_Student_Subject_Class
    /// - Sinh viên đăng ký môn => Training_Student_Subject
    /// - Loại điểm cho lớp học phần
    /// - Thời gian giảng viên => Personnel_Personnel_Session
    /// 
    /// Các chức năng chưa làm
    ///     Xem lại danh sách sinh viên
    ///     Sửa thông tin lớp học phần
    ///     Hiện thị các yêu cầu của mình
    ///     Tô màu
    ///     Xem lại toàn bộ theo thời gian
    ///     Ràng buộc môn học
    ///     Lọch sinh viên đã đưa qua
    ///     
    /// </summary>
    public partial class xucWeek : DevExpress.XtraEditors.XtraUserControl
    {
        #region Khai Báo Biến Toàn Cục
        public DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm(); 
        private DBMain m_dbTraining = new DBMain();
        private bds m_bds = new bds();

        private DateTime m_dteStart = new DateTime(1, 1, 1);
        private DateTime m_dteEnd = new DateTime(1, 1, 1);

        DateTime _dteStart = SysDateTime.Now;

        private xucDay m_xDayMo;
        private xucDay m_xDayTu;
        private xucDay m_xDayWe;
        private xucDay m_xDayTh;
        private xucDay m_xDayFr;
        private xucDay m_xDaySa;
        private xucDay m_xDaySu;
        private xucDay m_xDayUn;

        //private xucDay[] m_arXucDay;

        private StyleLoad m_styleLoad = StyleLoad.Normal;
        public delegate void onAddClass(xucClass obj);

        [Browsable(true)]
        [Category("LAU Control")]
        public event onAddClass OnAddClass;
        
        public delegate void onSaveClass(xucDay _Day, xucSession _Session, xucClass _Class);

        [Browsable(true)]
        [Category("LAU Control")]
        public event onSaveClass OnSaveClass;

        private List<xucClass> m_lXucClass = new List<xucClass>();
        #endregion

        #region Khởi Tạo Biến
        public xucWeek()
        {
            if (!SysUtils.CheckAccessByRule("R023") && 
                !SysUtils.CheckAccessByRule("R024") && 
                !SysUtils.CheckAccessByRule("R025") && 
                !SysUtils.CheckAccessByRule("R026"))
            {
                //XtraMessageBox.Show("Bạn không có quyền này");
                //return;
            }
            dlg.Show();
            InitializeComponent();

            //CompTime();

            #region Add day
            m_xDayMo = new xucDay(xucDay.DayOfWeek.Mo, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDayTu = new xucDay(xucDay.DayOfWeek.Tu, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDayWe = new xucDay(xucDay.DayOfWeek.We, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDayTh = new xucDay(xucDay.DayOfWeek.Th, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDayFr = new xucDay(xucDay.DayOfWeek.Fr, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDaySa = new xucDay(xucDay.DayOfWeek.Sa, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDaySu = new xucDay(xucDay.DayOfWeek.Su, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            m_xDayUn = new xucDay(xucDay.DayOfWeek.Un, dteStart.EditValue.ToDateTime(), dteEnd.EditValue.ToDateTime());
            #endregion
            
            #region Add Session
            this.scb_Main.Controls.Add(m_xDayMo);
            this.scb_Main.Controls.Add(m_xDayTu);
            this.scb_Main.Controls.Add(m_xDayWe);
            this.scb_Main.Controls.Add(m_xDayTh);
            this.scb_Main.Controls.Add(m_xDayFr);
            this.scb_Main.Controls.Add(m_xDaySa);
            this.scb_Main.Controls.Add(m_xDaySu);
            this.scb_Main.Controls.Add(m_xDayUn);
            #endregion

            #region Resize lic_Day Lic_Session
            lic_btn_Add_2_1.Width = m_xDayMo.Session1.Width;
            lic_btn_Add_2_2.Width = m_xDayMo.Session2.Width;
            lic_btn_Add_2_3.Width = m_xDayMo.Session3.Width;
            
            lic_btn_Add_3_1.Width = m_xDayTu.Session1.Width;
            lic_btn_Add_3_2.Width = m_xDayTu.Session2.Width;
            lic_btn_Add_3_3.Width = m_xDayTu.Session3.Width;
            
            lic_btn_Add_4_1.Width = m_xDayWe.Session1.Width;
            lic_btn_Add_4_2.Width = m_xDayWe.Session2.Width;
            lic_btn_Add_4_3.Width = m_xDayWe.Session3.Width;
            
            lic_btn_Add_5_1.Width = m_xDayTh.Session1.Width;
            lic_btn_Add_5_2.Width = m_xDayTh.Session2.Width;
            lic_btn_Add_5_3.Width = m_xDayTh.Session3.Width;
            
            lic_btn_Add_6_1.Width = m_xDayFr.Session1.Width;
            lic_btn_Add_6_2.Width = m_xDayFr.Session2.Width;
            lic_btn_Add_6_3.Width = m_xDayFr.Session3.Width;
            
            lic_btn_Add_7_1.Width = m_xDaySa.Session1.Width;
            lic_btn_Add_7_2.Width = m_xDaySa.Session2.Width;
            lic_btn_Add_7_3.Width = m_xDaySa.Session3.Width;
            lic_btn_Add_1_1.Width = m_xDaySu.Session1.Width;
            lic_btn_Add_1_2.Width = m_xDaySu.Session2.Width;
            lic_btn_Add_1_3.Width = m_xDaySu.Session3.Width;
            
            lic_btn_Add_0_1.Width = m_xDayUn.Session1.Width;
            lic_btn_Add_0_2.Width = m_xDayUn.Session2.Width;
            lic_btn_Add_0_3.Width = m_xDayUn.Session3.Width;
            #endregion

            #region Clock Size
            this.lic_btn_Add_2_1.MaxSize = new System.Drawing.Size(m_xDayMo.Session1.Width, 40);
            this.lic_btn_Add_2_1.MinSize = new System.Drawing.Size(m_xDayMo.Session1.Width, 40);
            this.lic_btn_Add_2_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_2_2.MaxSize = new System.Drawing.Size(m_xDayMo.Session2.Width, 40);
            this.lic_btn_Add_2_2.MinSize = new System.Drawing.Size(m_xDayMo.Session2.Width, 40);
            this.lic_btn_Add_2_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_2_3.MaxSize = new System.Drawing.Size(m_xDayMo.Session3.Width, 40);
            this.lic_btn_Add_2_3.MinSize = new System.Drawing.Size(m_xDayMo.Session3.Width, 40);
            this.lic_btn_Add_2_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_3_1.MaxSize = new System.Drawing.Size(m_xDayTu.Session1.Width, 40);
            this.lic_btn_Add_3_1.MinSize = new System.Drawing.Size(m_xDayTu.Session1.Width, 40);
            this.lic_btn_Add_3_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_3_2.MaxSize = new System.Drawing.Size(m_xDayTu.Session2.Width, 40);
            this.lic_btn_Add_3_2.MinSize = new System.Drawing.Size(m_xDayTu.Session2.Width, 40);
            this.lic_btn_Add_3_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_3_3.MaxSize = new System.Drawing.Size(m_xDayTu.Session3.Width, 40);
            this.lic_btn_Add_3_3.MinSize = new System.Drawing.Size(m_xDayTu.Session3.Width, 40);
            this.lic_btn_Add_3_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_4_1.MaxSize = new System.Drawing.Size(m_xDayWe.Session1.Width, 40);
            this.lic_btn_Add_4_1.MinSize = new System.Drawing.Size(m_xDayWe.Session1.Width, 40);
            this.lic_btn_Add_4_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_4_2.MaxSize = new System.Drawing.Size(m_xDayWe.Session2.Width, 40);
            this.lic_btn_Add_4_2.MinSize = new System.Drawing.Size(m_xDayWe.Session2.Width, 40);
            this.lic_btn_Add_4_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_4_3.MaxSize = new System.Drawing.Size(m_xDayWe.Session3.Width, 40);
            this.lic_btn_Add_4_3.MinSize = new System.Drawing.Size(m_xDayWe.Session3.Width, 40);
            this.lic_btn_Add_4_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_5_1.MaxSize = new System.Drawing.Size(m_xDayTh.Session1.Width, 40);
            this.lic_btn_Add_5_1.MinSize = new System.Drawing.Size(m_xDayTh.Session1.Width, 40);
            this.lic_btn_Add_5_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_5_2.MaxSize = new System.Drawing.Size(m_xDayTh.Session2.Width, 40);
            this.lic_btn_Add_5_2.MinSize = new System.Drawing.Size(m_xDayTh.Session2.Width, 40);
            this.lic_btn_Add_5_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_5_3.MaxSize = new System.Drawing.Size(m_xDayTh.Session3.Width, 40);
            this.lic_btn_Add_5_3.MinSize = new System.Drawing.Size(m_xDayTh.Session3.Width, 40);
            this.lic_btn_Add_5_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_6_1.MaxSize = new System.Drawing.Size(m_xDayFr.Session1.Width, 40);
            this.lic_btn_Add_6_1.MinSize = new System.Drawing.Size(m_xDayFr.Session1.Width, 40);
            this.lic_btn_Add_6_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_6_2.MaxSize = new System.Drawing.Size(m_xDayFr.Session2.Width, 40);
            this.lic_btn_Add_6_2.MinSize = new System.Drawing.Size(m_xDayFr.Session2.Width, 40);
            this.lic_btn_Add_6_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_6_3.MaxSize = new System.Drawing.Size(m_xDayFr.Session3.Width, 40);
            this.lic_btn_Add_6_3.MinSize = new System.Drawing.Size(m_xDayFr.Session3.Width, 40);
            this.lic_btn_Add_6_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            this.lic_btn_Add_7_1.MaxSize = new System.Drawing.Size(m_xDaySa.Session1.Width, 40);
            this.lic_btn_Add_7_1.MinSize = new System.Drawing.Size(m_xDaySa.Session1.Width, 40);
            this.lic_btn_Add_7_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_7_2.MaxSize = new System.Drawing.Size(m_xDaySa.Session2.Width, 40);
            this.lic_btn_Add_7_2.MinSize = new System.Drawing.Size(m_xDaySa.Session2.Width, 40);
            this.lic_btn_Add_7_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_7_3.MaxSize = new System.Drawing.Size(m_xDaySa.Session3.Width, 40);
            this.lic_btn_Add_7_3.MinSize = new System.Drawing.Size(m_xDaySa.Session3.Width, 40);
            this.lic_btn_Add_7_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;


            this.lic_btn_Add_1_1.MaxSize = new System.Drawing.Size(m_xDaySu.Session1.Width, 40);
            this.lic_btn_Add_1_1.MinSize = new System.Drawing.Size(m_xDaySu.Session1.Width, 40);
            this.lic_btn_Add_1_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_1_2.MaxSize = new System.Drawing.Size(m_xDaySu.Session2.Width, 40);
            this.lic_btn_Add_1_2.MinSize = new System.Drawing.Size(m_xDaySu.Session2.Width, 40);
            this.lic_btn_Add_1_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_1_3.MaxSize = new System.Drawing.Size(m_xDaySu.Session3.Width, 40);
            this.lic_btn_Add_1_3.MinSize = new System.Drawing.Size(m_xDaySu.Session3.Width, 40);
            this.lic_btn_Add_1_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;            

            this.lic_btn_Add_0_1.MaxSize = new System.Drawing.Size(m_xDayUn.Session1.Width, 40);
            this.lic_btn_Add_0_1.MinSize = new System.Drawing.Size(m_xDayUn.Session1.Width, 40);
            this.lic_btn_Add_0_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_0_2.MaxSize = new System.Drawing.Size(m_xDayUn.Session2.Width, 40);
            this.lic_btn_Add_0_2.MinSize = new System.Drawing.Size(m_xDayUn.Session2.Width, 40);
            this.lic_btn_Add_0_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.lic_btn_Add_0_3.MaxSize = new System.Drawing.Size(m_xDayUn.Session3.Width, 40);
            this.lic_btn_Add_0_3.MinSize = new System.Drawing.Size(m_xDayUn.Session3.Width, 40);
            this.lic_btn_Add_0_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_01.MaxSize = new System.Drawing.Size(25, 0);
            this.esi_emty_01.MinSize = new System.Drawing.Size(25, 0);
            this.esi_emty_01.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_1.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_1.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_2.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_2.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_3.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_3.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_4.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_4.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_4.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_5.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_5.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_5.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            
            this.esi_emty_6.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_6.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_6.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;

            this.esi_emty_7.MaxSize = new System.Drawing.Size(28, 0);
            this.esi_emty_7.MinSize = new System.Drawing.Size(28, 0);
            this.esi_emty_7.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            #endregion

            #region Show Day Even on Day
            this.m_xDayMo.Show();
            this.m_xDayTu.Show();
            this.m_xDayWe.Show();
            this.m_xDayTh.Show();
            this.m_xDayFr.Show();
            this.m_xDaySa.Show();
            this.m_xDaySu.Show();
            this.m_xDayUn.Show();

            this.m_xDayMo.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDayTu.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDayWe.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDayTh.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDayFr.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDaySa.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDaySu.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);
            this.m_xDayUn.OnAddClass += new xucDay.onAddClass(Event_OnAddClass);

            this.m_xDayMo.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDayTu.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDayWe.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDayTh.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDayFr.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDaySa.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDaySu.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);
            this.m_xDayUn.OnChangeDay += new xucDay.onChangeDay(Event_OnChangeDay);

            this.m_xDayMo.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDayTu.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDayWe.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDayTh.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDayFr.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDaySa.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDaySu.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            this.m_xDayUn.OnChooseChange += new xucDay.onChooseChange(Event_OnChooseChange);
            #endregion 
            try
            {
                this.dlg.Hide();
                this.Dock = DockStyle.Fill;
                PersmissionShow();
            }
            catch
            {
            }

            m_dteStart = dteStart.DateTime;
            m_dteEnd = dteEnd.DateTime;
        }

        #region Biến Ngày Giờ
        public enum StyleLoad
        {
            Normal, JoinRequest,
        }

        public DateTime DteStart
        {
            get { return this.m_dteStart; }
        }

        public DateTime DteEnd
        {
            get { return this.m_dteEnd; }
        }

        public xucDay MoDay
        {
            get { return this.m_xDayMo; }
        }

        public xucDay TuDay
        {
            get { return this.m_xDayTu; }
        }

        public xucDay WeDay
        {
            get { return this.m_xDayWe; }
        }

        public xucDay ThDay
        {
            get { return this.m_xDayTh; }
        }

        public xucDay FrDay
        {
            get { return this.m_xDayFr; }
        }

        public xucDay SaDay
        {
            get { return this.m_xDaySa; }
        }

        public xucDay SuDay
        {
            get { return this.m_xDaySu; }
        }

        public xucDay UnDay
        {
            get { return this.m_xDayUn; }
        }
        #endregion
        #endregion

        #region Sự Kiện xuc
        private void xucWeek_Load(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < this.scb_Main.Controls.Count; i++)
                {
                    if (this.scb_Main.Controls[i].GetType().Name == "xucDay")
                    {
                        this.scb_Main.Controls[i].Height = this.scb_Main.Height;
                        this.scb_Main.Controls[i].Controls[0].Height = this.scb_Main.Height;
                    }
                }
                ReloadData();
            }
            catch
            {
            }
        }

        private void PersmissionShow()
        {
            if (SysUtils.CheckAccessByRule("R023") || 
                SysUtils.CheckAccessByRule("R024") || 
                SysUtils.CheckAccessByRule("R025") || 
                SysUtils.CheckAccessByRule("R026"))
            {
            }
            else
            {
                try
                {
                    this.btnView.Visible = false;
                }
                catch
                {
                }
            }
            if (!SysUtils.CheckEditByRule("R025"))
            {
                try
                {
                    layoutControlItem4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }
                catch
                {
                }
            }
            if (!SysUtils.CheckEditByRule(Utils.Configs.RuleConfig.SUBJECTOPEN))
            {
                try
                {
                layoutControlItem7.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }
                catch
                {
                }
            }
        }        

        private void Event_OnAddClass(xucClass obj)
        {
            try
            {
                if (OnAddClass != null) OnAddClass(obj);
            }
            catch
            {
            }
        }

        private void Event_OnChangeDay(xucDay _xucDay, xucSession _xucSesion,xucClass _xucClass)
        {
            try
            {
            MoDay.AddClass(_xucClass);
            TuDay.AddClass(_xucClass);
            WeDay.AddClass(_xucClass);
            ThDay.AddClass(_xucClass);
            FrDay.AddClass(_xucClass);
            SaDay.AddClass(_xucClass);
            SuDay.AddClass(_xucClass);
            UnDay.AddClass(_xucClass);
            }
            catch
            {
            }
        }

        private void Event_OnChooseChange(xucDay _xucDay, xucSession _xucSesion, xucClass _xucClass)
        {
            if (_xucClass.Checked)
            {
                try
                {
                    this.m_lXucClass.Add(_xucClass);
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                this.m_lXucClass.Remove(_xucClass);
                }
                catch
                {
                }
            }
        }
        #endregion

        #region Sự Kiện btn
        private void btnView_Click(object sender, EventArgs e)
        {
            ViewAll();
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            try
            {
                if (!SysUtils.CheckEditByRule(Utils.Configs.RuleConfig.SUBJECTOPEN))
                {
                    XtraMessageBox.Show(Utils.Configs.RuleConfig.MESSAGEBOX);
                    return;
                }
                if (XtraMessageBox.Show("Bạn có chắc chắn không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.m_dbTraining.m_DCReadOnly.Training_SubjectOpenRequests.UpdateFinish())
                    {
                        XtraMessageBox.Show("Đóng thành công");
                    }
                    else
                    {
                        XtraMessageBox.Show("Thất bại, kiểm tra đã duyệt toàn bộ chưa");
                    }
                }
            }
            catch
            {
            }
        }        

        private void btnJoin_Click(object sender, EventArgs e)
        {
            //try
            {
                if (!SysUtils.CheckAccessByRule("R025"))
                {
                    //XtraMessageBox.Show("Bạn không có quyền này");
                    return;
                }
                if (XtraMessageBox.Show("Bạn có chắc chắn muốn ghép lớp?", "Xác nhận", MessageBoxButtons.YesNo)
                    == DialogResult.Yes)
                {
                    //try
                    {
                        this.dlg.Show();
                        JoinRequest();
                        this.dlg.Hide();
                    }
                    //catch
                    {
                    }
                }
            }
            //catch
            {
            }
        }

        private void btnSplitRequest_Click(object sender, EventArgs e)
        {
            try
            {
                if (!SysUtils.CheckEditByRule(Utils.Configs.RuleConfig.JOINSUBJECTREQUESTOPEN))
                {
                    XtraMessageBox.Show(Utils.Configs.RuleConfig.MESSAGEBOX);
                    return;
                }
                if (XtraMessageBox.Show("Bạn có chắc chắn muốn tách?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        this.dlg.Show();
                        for (int i = 0; i < this.m_lXucClass.Count; i++)
                        {
                            int iRequestKey = this.m_lXucClass[i].Training_SubjectOpenRequestKey;
                            this.m_dbTraining.m_DCSave.Training_SubjectOpenRequests.SplitRequestJoined(iRequestKey);
                        }
                        ReloadData();
                        this.dlg.Hide();
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                //_dteStart = dteStart.DateTime;
                //CompTime();
                //m_dteStart = dteStart.DateTime;
                //m_dteEnd = dteEnd.DateTime;
            }
            catch
            {
            }
        }

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            //m_dteEnd = dteEnd.DateTime;
        }
        #endregion

        #region Hàm Sữ Dụng
        private void ViewAll()
        {
            //Cache.m_dteLuuTam1 = this.dteStart.DateTime;
            //Cache.m_dteLuuTam2 = this.dteEnd.DateTime;
            //try
            //{
                //if (SysUtils.CheckAccessByRule("R023") ||
                //    SysUtils.CheckAccessByRule("R024") ||
                //    SysUtils.CheckAccessByRule("R025") ||
                //    SysUtils.CheckAccessByRule("R026"))
                //{
                    //if (dteStart.DateTime != new DateTime() &&
                    //    dteEnd.DateTime != new DateTime())
                    //{
                        //if (dteStart.DateTime <= dteEnd.DateTime
                        //    //&& dteStart.DateTime >= Lib.Sys.SysDateTime.Now
                        //    && dteEnd.DateTime >= Lib.Sys.SysDateTime.Now)
                        //{
                                ReloadData();
                        //}
                        //else
                        //{
                        //    MessageBox.Show("        Chọn Ngày Sai \nNgày chọn phải lớn hơn ngày hiện tại" +
                        //                    "\nNgày bắt đầu phải trước ngày kết thúc", "Thông Báo");
                        //}
                    //}
                    //else
                    //{
                    //    MessageBox.Show("Bạn Chưa Chọn Ngày", "Thông Báo");
                    //}
                //}
                //else
                //{
                    //XtraMessageBox.Show("Bạn không có quyền này");
                //}
            //}
            //catch
            //{
            //}
        }

        private bool Check_Add()
        {
            try
            {
                if (!SysUtils.CheckAddByRule(Utils.Configs.RuleConfig.SUBJECTOPENREQUEST))
                {
                    XtraMessageBox.Show("Bạn không có quyền này");
                    return false;
                }
                else return true;
            }
            catch
            {
                return false;
            }
        }

        private void AddClass(int _iDayOfWeek, int _iSession, int _iTraining_SubjectOpenRequestKey)
        {
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Mo)
            {
                try
                {
                    m_xDayMo.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Tu)
            {
                try
                {
                    m_xDayTu.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.We)
            {
                try
                {
                    m_xDayWe.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Th)
            {
                try
                {
                    m_xDayTh.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Fr)
            {
                try
                {
                    m_xDayFr.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Sa)
            {
                try
                {
                    m_xDaySa.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Su)
            {
                try
                {
                    m_xDaySu.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
            if (_iDayOfWeek == (int)xucDay.DayOfWeek.Un)
            {
                try
                {
                    m_xDayUn.AddClass(_iSession, _iTraining_SubjectOpenRequestKey);
                }
                catch
                {
                }
            }
        }

        private void JoinRequest()
        {
            int iDayOfWeek = -1;
            int iSession = -1;
            if (this.m_lXucClass.Count > 1)
            {
                //try
                {
                    iDayOfWeek = this.m_lXucClass[0].DayOfWeek;
                    iSession = this.m_lXucClass[0].Session;
                    int iParentKey = this.m_lXucClass[0].Training_SubjectOpenRequestKey;
                    int iRoomKey = this.m_lXucClass[0].Room;

                    Training_SubjectOpenRequestInfo info = 
                        new Training_SubjectOpenRequestInfo(this.m_lXucClass[0].Training_SubjectOpenRequestKey);
                    int subjectGroup = Convert.ToInt16(info.Properties.Training_SubjectKey.ToString());

                    Training_SubjectInfo subject = new Training_SubjectInfo(subjectGroup);
                    if(subject.Properties.Subject_SameKey.ToString() != "")
                    {
                        subjectGroup = Convert.ToInt16(subject.Properties.Subject_SameKey.ToString());
                    }

                    subject.Properties.Subject_SameKey = subjectGroup;
                    subject.Save();

                    for (int i = 0; i < this.m_lXucClass.Count; i++)
                    {
                        if (this.m_lXucClass[i].DayOfWeek != iDayOfWeek || this.m_lXucClass[i].Session != iSession)
                        {
                            XtraMessageBox.Show("Các yêu cầu được chọn phải giống nhau về ngày học và ca");
                            return;
                        }
                    }
                    xucDay _xucDay = null;
                    xucSession _xucSession = null;
                    #region
                    //for (int i = 0; i < this.ListDay.Length; i++)
                    //{
                    //    if (this.ListDay[i].iDayOfWeek == iDayOfWeek)
                    //    {
                    //        _xucDay = this.ListDay[i];
                    //        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                    //        {
                    //            if (_xucDay.ListSession[j].iSession == iSession)
                    //            {
                    //                _xucSession = _xucDay.ListSession[j];
                    //                break;
                    //            }
                    //        }
                    //        break;
                    //    }
                    //}
                    if (MoDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = MoDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (TuDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = TuDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (WeDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = WeDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (ThDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = ThDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (FrDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = FrDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (SaDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = SaDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (SuDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = SuDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }

                    if (UnDay.iDayOfWeek == iDayOfWeek)
                    {
                        _xucDay = UnDay;
                        for (int j = 0; j < _xucDay.ListSession.Length; j++)
                        {
                            if (_xucDay.ListSession[j].iSession == iSession)
                            {
                                _xucSession = _xucDay.ListSession[j];
                                break;
                            }
                        }
                        goto aa;
                    }
                    #endregion
                aa:
                    if (_xucSession != null)
                    {
                        for (int i = 1; i < this.m_lXucClass.Count; i++)
                        {
                            if (m_lXucClass[i].listChildrend.Rows.Count > 0)
                            {
                                for (int k = 0; k < m_lXucClass[i].listChildrend.Rows.Count; k++)
                                {
                                    GetJoin(
                                        Convert.ToInt16(
                                        m_lXucClass[i].listChildrend.Rows[k]["Training_SubjectOpenRequestKey"].ToString()),
                                        iRoomKey,
                                        iParentKey,
                                        _xucSession,
                                         i, 
                                         subjectGroup);
                                }
                            }
                            else
                            {
                                GetJoin(
                                    m_lXucClass[i].Training_SubjectOpenRequestKey,
                                    iRoomKey,
                                    iParentKey,
                                    _xucSession,
                                    i,
                                    subjectGroup);
                            }
                        }
                    }
                }
                //catch
                {
                }
            }
            else
            {
                XtraMessageBox.Show("Số yêu cầu chọn phải lớn hơn 1 yêu cầu");
                return;
            }
        }

        private void ReloadData()
        {
            try
            {
                Cache.m_dteStartDay = this.m_dteStart;
                Cache.m_dteEndDay = this.m_dteEnd;
                #region add session
                for (int j = 0; j < this.MoDay.ListSession.Length; j++)
                {
                    this.MoDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.TuDay.ListSession.Length; j++)
                {
                    this.TuDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.WeDay.ListSession.Length; j++)
                {
                    this.WeDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.ThDay.ListSession.Length; j++)
                {
                    this.ThDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.FrDay.ListSession.Length; j++)
                {
                    this.FrDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.SaDay.ListSession.Length; j++)
                {
                    this.SaDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.SuDay.ListSession.Length; j++)
                {
                    this.SuDay.ListSession[j].ClearAllClass();
                }
                for (int j = 0; j < this.UnDay.ListSession.Length; j++)
                {
                    this.UnDay.ListSession[j].ClearAllClass();
                }
                #endregion
                dgv_drag.DataSource = 
                    this.m_dbTraining.m_DCReadOnly.Training_SubjectOpenRequests.GetListByDateTime(
                        this.m_dteStart,
                        this.m_dteEnd);
                if (dgv_drag.RowCount > 0)
                {
                    try
                    {
                        this.m_styleLoad = StyleLoad.JoinRequest;
                        Cache.m_styleLoad = this.m_styleLoad;
                        for (int i = 0; i < dgv_drag.RowCount; i++)
                        {
                            int iTraining_SubjectOpenRequestKey = 
                                dgv_drag.Rows[i].Cells["Training_SubjectOpenRequestKey"].Value.ToInt();
                            int iDayOfWeek = 
                                dgv_drag.Rows[i].Cells["Training_WeekDayKey"].Value.ToInt();
                            int iTraining_SessionKey = 
                                dgv_drag.Rows[i].Cells["Training_SessionKey"].Value.ToInt();
                            AddClass(
                                iDayOfWeek, 
                                iTraining_SessionKey, 
                                iTraining_SubjectOpenRequestKey);
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    XtraMessageBox.Show("Không có yêu cầu nào");
                }
            }
            catch
            {
            }
        }

        private void GetJoin(
            int Training_SubjectOpenRequestKey,
            int iRoomKey,
            int iParentKey,
            xucSession _xucSession,
            int i,
            int subjectGroup)
        {
            Training_SubjectOpenRequestInfo subjectOpenRequestInfo =
                        new Training_SubjectOpenRequestInfo(Training_SubjectOpenRequestKey);
            subjectOpenRequestInfo.Properties.Training_SubjectOpenRequestKeyParent = iParentKey;
            if (iRoomKey != 0)
            {
                subjectOpenRequestInfo.Properties.Training_Class_RoomKey = iRoomKey;
            }
            subjectOpenRequestInfo.Save();

            bds bd = new bds();
            bd.DataSource = 
                m_dbTraining.m_DCSave.Training_Subjects.GetListSameBySubjectKey(
                Convert.ToInt16(subjectOpenRequestInfo.Properties.Training_SubjectKey.ToString()));
            gctdraf.DataSource = bd;

            for (int k = 0; k < grvdraf.RowCount; k++)
            {
                grvdraf.SetRowCellValue(i, "Subject_SameKey", subjectGroup);
            }

            m_dbTraining.m_DCSave.SubmitChanges();

             Training_SubjectInfo sj = new Training_SubjectInfo(
                Convert.ToInt16(subjectOpenRequestInfo.Properties.Training_SubjectKey.ToString()));
            sj.Properties.Subject_SameKey=subjectGroup;
            sj.Save();

            DataTable dt =
                this.m_dbTraining.m_DCReadOnly.
                Training_SubjectOpenRequest_Teacher_Students.GetByReuqestOpenKey(
                   this.m_dbTraining.m_DCReadOnly.Training_Classes,
                   Training_SubjectOpenRequestKey).ToDataTable();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                this.m_lXucClass[0].ClassesTable.Rows.Add(
                    dt.Rows[j][0],
                    dt.Rows[j][1],
                    dt.Rows[j][2],
                    dt.Rows[j][3]);
            }
            _xucSession.HideClass(this.m_lXucClass[i]);
            this.m_lXucClass[0].Checked = false;
            this.m_lXucClass.Clear();
        }

        public static void SetupScrollState(ScrollableControl scrollableControl)
        {
            Type scrollableControlType = typeof(ScrollableControl);
            MethodInfo setScrollStateMethod = scrollableControlType.GetMethod(
                "SetScrollState", BindingFlags.NonPublic | BindingFlags.Instance);
            setScrollStateMethod.Invoke(scrollableControl, 
                new object[] { ScrollableControl.ScrollStateFullDrag, true });
        }

        //private void CompTime()
        //{
        //    if ((int)_dteStart.DayOfWeek != 1)
        //    {
        //        _dteStart = _dteStart - TimeSpan.FromDays((int)_dteStart.DayOfWeek - 1);
        //    }
        //    this.dteStart.EditValue = _dteStart;
        //    m_dteStart = dteStart.DateTime;
        //    m_dteStart = _dteStart;

        //    _dteStart = _dteStart.AddDays(7 * 15);
        //    this.dteEnd.EditValue = _dteStart;
        //    m_dteEnd = dteEnd.DateTime;

        //    m_dteStart = dteStart.DateTime;
        //    m_dteEnd = dteEnd.DateTime;
        //}

        //private bool Check_Dte()
        //{
        //    try
        //    {
        //        if (dteStart.DateTime > dteEnd.DateTime
        //            || dteStart.DateTime < Lib.Sys.SysDateTime.Now
        //            || dteEnd.DateTime < Lib.Sys.SysDateTime.Now)
        //        {
        //            return false;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}
        #endregion

        #region Btn Add
        private void btnAdd_2_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayMo.Session1.AddClass();
            }
        }

        private void btnAdd_3_1_Click(object sender, EventArgs e)
        {            
            if (Check_Add())
            {
                m_xDayTu.Session1.AddClass();
            }
        }

        private void btnAdd_4_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayWe.Session1.AddClass();
            }
        }

        private void btnAdd_5_1_Click(object sender, EventArgs e)
        {            if (Check_Add())
            {
                m_xDayTh.Session1.AddClass();
            }
        }

        private void btnAdd_6_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayFr.Session1.AddClass();
            }
        }

        private void btnAdd_7_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySa.Session1.AddClass();
            }
        }

        private void btnAdd_1_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySu.Session1.AddClass();
            }
        }

        private void btnAdd_0_1_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayUn.Session1.AddClass();
            }
        }

        private void btnAdd_2_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayMo.Session2.AddClass();
            }
        }

        private void btnAdd_3_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayTu.Session2.AddClass();
            }
        }

        private void btnAdd_4_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayWe.Session2.AddClass();
            }
        }

        private void btnAdd_5_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayTh.Session2.AddClass();
            }
        }

        private void btnAdd_6_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayFr.Session2.AddClass();
            }
        }

        private void btnAdd_7_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySa.Session2.AddClass();
            }
        }

        private void btnAdd_1_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySu.Session2.AddClass();
            }
        }

        private void btnAdd_0_2_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayUn.Session2.AddClass();
            }
        }

        private void btnAdd_2_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayMo.Session3.AddClass();
            }
        }

        private void btnAdd_3_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayTu.Session3.AddClass();
            }
        }

        private void btnAdd_4_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayWe.Session3.AddClass();
            }
        }

        private void btnAdd_5_3_Click(object sender, EventArgs e)
        {

            if (Check_Add())
            {
                m_xDayTh.Session3.AddClass();
            }
        }

        private void btnAdd_6_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayFr.Session3.AddClass();
            }
        }

        private void btnAdd_7_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySa.Session3.AddClass();
            }
        }

        private void btnAdd_1_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDaySu.Session3.AddClass();
            }
        }

        private void btnAdd_0_3_Click(object sender, EventArgs e)
        {
            if (Check_Add())
            {
                m_xDayUn.Session3.AddClass();
                scb_Main.Select();
                scb_Main.SetAutoScrollMargin(m_xDayUn.Session3.Location.X, m_xDayUn.Session3.Location.Y);
            }
        }
        #endregion 
    }
}
